package com.ssy.lingxi.marketing.controller.feign;

import cn.hutool.core.collection.CollectionUtil;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.marketing.api.model.request.*;
import com.ssy.lingxi.marketing.api.model.response.CartActivityPriceFeignResponse;
import com.ssy.lingxi.marketing.api.model.response.GoodsCartResponse;
import com.ssy.lingxi.marketing.api.model.response.ProductTagResp;
import com.ssy.lingxi.marketing.service.IActivityGoodsCacheService;
import com.ssy.lingxi.marketing.service.IActivityOrderService;
import com.ssy.lingxi.marketing.service.IActivityPriceCalculateService;
import com.ssy.lingxi.marketing.service.feign.IActivityGoodsFeignService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 活动商品feign接口
 * @author yzc
 * @version 2.0.0
 * @date 2021/9/2
 */
@RestController
public class ActivityGoodsFeignController {

    @Resource
    private IActivityPriceCalculateService activityPriceCalculateService;
    @Resource
    private IActivityGoodsCacheService activityGoodsCacheService;
    @Resource
    private IActivityGoodsFeignService activityGoodsFeignService;
    @Resource
    private IActivityOrderService activityOrderService;

    /**
     * 内部接口 - 查询活动商品活动标签
     **/
    @PostMapping(value = "/marketing/activity/feign/goods/product/tag")
    public Wrapper<List<ProductTagResp>> listActivityGoodsProductTag(@RequestBody ProductTagReq req) {
        return activityGoodsFeignService.listActivityGoodsProductTag(req);
    }

    /**
     * 内部接口 - 进货单获取sku商品活动
     **/
    @PostMapping(value = "/marketing/activity/feign/goods/cart/activity")
    public Wrapper<List<GoodsCartResponse>> listActivityGoodsCart(@Valid @RequestBody GoodsCartRequest request) {
        return activityGoodsFeignService.listActivityGoodsCart(request);
    }

    /**
     * 获取商品的最大优惠价格
     **/
    @PostMapping(value = "/marketing/activity/feign/goods/getPreferentialPrice")
    public Wrapper<List<ProductTagResp>> getPreferentialPrice(@RequestBody ProductTagReq req) {
        return activityGoodsFeignService.getPreferentialPrice(req);
    }
    /**
     * 更新活动商品销量
     **/
    @PostMapping(value = "/marketing/feign/activity/goods/sales/update")
    public Wrapper<Void> salesUpdate(@Valid @RequestBody GoodsSalesReq req) {
        return activityGoodsCacheService.updateGoodsSales(req);
    }

    /**
     * 内部接口 - 计算活动到手价
     **/
    @PostMapping(value = "/marketing/feign/activity/goods/price/calculate")
    public Wrapper<List<CartActivityPriceFeignResponse>> getPreferentialPrice(@Valid @RequestBody CartActivityPriceFeignRequest request) {
        if(CollectionUtil.isEmpty(request.getSkuList())){return Wrapper.success();}
        UserLoginCacheDTO user=new UserLoginCacheDTO();
        user.setMemberId(request.getMemberId());
        user.setMemberRoleId(request.getRoleId());
        return activityPriceCalculateService.calculateActivityPriceByFeign(user,request.getSkuList());
    }

    /**
     * 添加拼团
     **/
    @PostMapping(value = "/marketing/feign/add/group/purchase")
    public Wrapper<?> addGroupPurchase(@Valid @RequestBody GroupPurchaseAddRequest req) {
        return activityOrderService.addGroupPurchase(req);
    }

    /**
     * 校验是否重复参与同一个拼团
     */
    @PostMapping(value = "/marketing/feign/is/repeat/join/group/purchase")
    public Wrapper<Boolean> isRepeatJoinGroupPurchase(@Valid @RequestBody GroupPurchaseCheckRequest req){
        return activityOrderService.isRepeatJoinGroupPurchase(req);
    }

}
